/*
This dofile replicates all the tables in the paper.
*/

capture log close
clear all
cap clear matrix
cap clear mata
cap clear eststo
set matsize 10000
set maxvar 30000
set more off, permanently

ssc install egenmore, replace
ssc install mdesc, replace
ssc install reghdfe, replace
ssc install ftools, replace
reghdfe, compile
ssc install tuples, replace
ssc install xtivreg2, replace
ssc install ivreg2, replace
ssc install ranktest, replace
ssc install ftools, replace
ssc install spmap, replace

global wkdir `c(pwd)'
global rawdata "${wkdir}/Data/Raw"
global regdata "${wkdir}/Data/RegData"

capture confirm file "${wkdir}/Results/Table"
if _rc mkdir "${wkdir}/Results/Table"
global out_tab "${wkdir}/Results/Table"

log using gen_tabs.log, replace	


**********************************************************************************
************************* TABLES FOR MAIN PAPER SECTION **************************
**********************************************************************************

************************************************************************************
*** Table 1: Effect of UI Generosity on UI Benefits and Insurance coverage - SIPP***
************************************************************************************
use "${regdata}/sipp_reg.dta", clear

xtset uniqueid order
local controls i.age female i.race i.edgr married i.imonth urate uratesq uratecu avweek* i.kids_mloss i.year_mloss ///
	i.statefip_mloss i.statefip_m*trend
	
foreach y in uiamt any_hins priv_hins pub_hins {
	xi: xtreg `y' sim_post post `controls'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estimates store r`y'
	estadd ysumm
}
esttab ruiamt rany_hins rpriv_hins rpub_hins using "${out_tab}/p1_reg_sipp_sykloss.tex", ///
	replace keep(sim_*) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant mtitle("Benefit" "Any" ///
	"Private" "Public") mgroups("Monthly UI" "Health Insurance", pattern(1 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) ///
	span erepeat(\cmidrule(lr){@span})) stats(ymean N, labels ("Mean Y" "Observations") fmt(2 0))
eststo clear


*******************************************************************************************************************
*** Table 2: Effect of UI Generosity on UI Benefits and Insurance coverage - Sensitivity to Specification - SIPP***
*******************************************************************************************************************
use "${regdata}/sipp_reg.dta", clear
cap drop ui_netre*
gen statefip = statefip_m0
gen year = year_m0
merge m:1 statefip year using "${regdata}/state_data.dta", keepus(ui_netres*)
keep if year>=1993 & year<=2015
drop _merge statefip year

for any epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: gen Xsq=X*X
for any epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: gen Xcu=X*X*X

egen state_year = group(statefip_m year_m)
egen kids_year=group(kids_m year_m)
egen kids_statefip=group(kids_m statefip_m)
egen state_yearloss = group(statefip_m year_m post)
egen kids_yearloss=group(kids_m year_m post)
egen kids_statefiploss=group(kids_m statefip_m post)

xtset uniqueid order
local controls i.age female i.race i.edgr married i.imonth urate uratesq uratecu avweek* i.kids_mloss i.year_mloss ///
	i.statefip_mloss i.statefip_m*trend
local controls3 i.age female i.race i.edgr married i.imonth urate uratesq uratecu avweek* i.kids_mloss i.year_mloss ///
	i.statefip_mloss
local controls4 i.age female i.race i.edgr married i.imonth i.kids_mloss i.year_mloss i.statefip_mloss i.statefip_m*trend 
local controls5 `controls' urate_lag1* urate_lag2* ui_netres_lag1_pop* ui_netres_lag2_pop* 
local controls6 `controls' minwage eitc_val reform afdcmax4 medthresh
local controls7 i.age female i.race i.edgr married i.imonth urate uratesq uratecu avweek* i.kids_m i.year_m i.statefip_m*trend
local controls8 `controls' i.kids_yearloss
local controls9 `controls' i.kids_statefiploss
local controls10 `controls' i.state_yearloss
local controls11 `controls' i.kids_yearloss i.kids_statefiploss i.state_yearloss

foreach y in uiamt any_hins {
	
	* Baseline
	eststo: xi: xtreg `y' sim_post post `controls'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	
	* No individual FEs
	eststo: xi: reg `y' sim_post sim_repl_sipp post `controls'  [pw=p5wgt_m0], vce(cluster statefip_m0)	
	estadd ysumm
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	
	* No Trends
	eststo: xi: xtreg `y' sim_post post `controls3'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local stecon="Yes"
	estadd local sykloss="Yes"
	
	* No Econ Conditions
	eststo: xi: xtreg `y' sim_post post `controls4'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local sttrend="Yes"
	
	* Additional Econ Conditions
	eststo: xi: xtreg `y' sim_post post `controls5'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	estadd local steconadd="Yes"
	
	* Safety Net
	eststo: xi: xtreg `y' sim_post post `controls6'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	estadd local safety="Yes"
	
	* No SYK*loss
	eststo: xi: xtreg `y' sim_post post `controls7'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	
	* Kids*Year*Loss
	eststo: xi: xtreg `y' sim_post post `controls8'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	estadd local ykloss_two="Yes"
	
	* Kids*State*Loss
	eststo: xi: xtreg `y' sim_post post `controls9'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	estadd local skloss_two="Yes"
	
	* Year*State*Loss
	eststo: xi: xtreg `y' sim_post post `controls10'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	estadd local syloss_two="Yes"
	
	* All two-way
	eststo: xi: xtreg `y' sim_post post `controls11'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
	estadd local indiv="Yes"
	estadd local sykloss="Yes"
	estadd local stecon="Yes"
	estadd local sttrend="Yes"
	estadd local ykloss_two="Yes"
	estadd local skloss_two="Yes"
	estadd local syloss_two="Yes"
	
	if "`y'"=="uiamt" {
		esttab using "${out_tab}/p2_reg_sipp_sens.tex", replace tex keep(sim_*) star(* 0.10 ** 0.05 *** 0.01) ///
			se(1) b(1) label numbers nonotes noobs noconstant nomtitles postfoot("") prefoot("") ///
			varlabels( , blist(sim_post "\midrule \it{\underline{A: Monthly UI (\\$ 2015)}} \\ "))
	}
	else {
		esttab using "${out_tab}/p2_reg_sipp_sens.tex", append tex keep(sim_*) se(3) b(3) label ///
			star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant noobs nomtitle posthead("") prehead("") stats(indiv sttrend  ///
			stecon steconadd safety sykloss ykloss_two skloss_two syloss_two, labels ("Individual FEs" ///
			"State Linear Trends" "State Economic Conditions" "Lagged State Economic Conditions" "State Safety Net" ///
			"State*Loss, Kids*Loss, Year*Loss FEs"  "Year*Kids*Loss FEs" "State*Kids*Loss FEs" "State*Year*Loss FEs")) ///
			varlabels( , blist(sim_post "\midrule \it{\underline{B: Any Health Insurance}} \\ "))
	}
	eststo clear
}


***********************************************************************************
*** Table 3: Effect of UI Generosity on Health Insurance and Utilization - BRFSS***
***********************************************************************************
use "${regdata}/brfss_reg.dta", clear

local controls female married urate uratesq uratecu avweek* 
local absvars1 i.age i.race i.edgr i.imonth i.kids i.year i.statefip i.statefip#c.trend
local absvars2 i.age i.race i.edgr i.imonth  i.kids_unemp i.year_unemp i.state_unemp i.statefip#c.trend

*** Unemployed
preserve
keep if unemployed==1
foreach y in plan check_lasty nafford bexamly brexam {
	eststo: reghdfe `y' sim_repl_sipp `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
	estadd ysumm		
}
esttab using "${out_tab}/p3_utilization.tex", replace keep(sim_repl_sipp) star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) nonum nonotes ///
	noconstant label mtitle("Health Insurance" "Checkup" "Afford Doctor" "Ever" "Last Year") mgroups("" "Breast Exam", ///
	pattern(1 0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) prefoot("") postfoot("") ///
	stats(ymean N, labels("Mean Y" "Observations") fmt(2 0)) varlabels( , blist(sim_repl_sipp ///
	"\midrule \it{\underline{A: Difference in Differences: Unemployed}} \\ "))
eststo clear
restore

*** DDD: No state-year fe
foreach y in plan check_lasty nafford bexamly brexam {
	eststo: reghdfe `y' sim_repl_sipp sim_unemp unemployed `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars2')
	estadd ysumm
}
esttab using "${out_tab}/p3_utilization.tex", append keep(sim_repl_sipp sim_unemp) star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) nonum nonotes ///
	noconstant nomtitle label posthead("") prehead("") postfoot("") prefoot("") stats(ymean N, labels("Mean Y" ///
	"Observations") fmt(2 0)) varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{B: Triple Differences}} \\ "))
eststo clear

*** DDD: With state-year-kids fe
foreach y in plan check_lasty nafford bexamly brexam {
	eststo: reghdfe `y' sim_unemp sim_repl_sipp unemployed `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars2' i.state_year_kids)
	estadd ysumm	
}
esttab using "${out_tab}/p3_utilization.tex", append keep(sim_unemp) star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) nonum nonotes ///
	noconstant nomtitle label posthead("") prehead("") prefoot("") stats(ymean N, labels("Mean Y" ///
	"Observations") fmt(2 0)) varlabels( , blist(sim_unemp ///
	"\midrule \it{\underline{C: Triple Differences, State*Year*Kids FE}} \\ "))	
eststo clear


***********************************************************************
*** Table 4: Effect of UI Generosity on Self-Reported Health - BRFSS***
***********************************************************************
use "${regdata}/brfss_reg.dta", clear

local controls female married urate uratesq uratecu avweek*
local absvars1 i.age i.race i.edgr i.imonth i.kids i.year i.statefip i.statefip#c.trend
local absvars2 i.age i.race i.edgr i.imonth  i.kids_unemp i.year_unemp i.state_unemp i.statefip#c.trend

*** Unemployed
preserve
keep if unemployed==1
foreach y in genhlth goodgenhlth excgenhlth physhlth goodphyshlth menthlth goodmenthlth {
	eststo: reghdfe `y' sim_repl_sipp `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
	estadd ysumm
}
esttab using "${out_tab}/p4_selfhealth.tex", replace keep(sim_repl_sipp) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) nonum nonotes noconstant label mtitle("Continuous" "Good" "Excellent" "Days" "Always" ///
	"Days" "Always") mgroups("General Health" "Physically Healthy" "Physically Healthy", ///
	pattern(1 0 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	prefoot("") postfoot("") stats(ymean N, labels("Mean Y" "Observations") fmt(2 0)) ///
	varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{A: Difference in Differences: Unemployed}} \\ "))
eststo clear
restore

*** DDD: No state-year fe
foreach y in genhlth goodgenhlth excgenhlth physhlth goodphyshlth menthlth goodmenthlth {
	eststo: reghdfe `y' sim_repl_sipp sim_unemp unemployed `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars2')
	estadd ysumm
}
esttab using "${out_tab}/p4_selfhealth.tex", append keep(sim_repl_sipp sim_unemp) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) nonum nonotes noconstant nomtitle label posthead("") prehead("") ///
	postfoot("") prefoot("") stats(ymean N, labels("Mean Y" "Observations") fmt(2 0)) ///
	varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{B: Triple Differences}} \\ "))
eststo clear

*** DDD: With state-year-kids fe
foreach y in genhlth goodgenhlth excgenhlth physhlth goodphyshlth menthlth goodmenthlth {
	eststo: reghdfe `y' sim_unemp sim_repl_sipp unemployed `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars2' i.state_year_kids) 
	estadd ysumm	
}
esttab using "${out_tab}/p4_selfhealth.tex", append keep(sim_unemp) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) nonum nonotes noconstant nomtitle label posthead("") prehead("") ///
	prefoot("") stats(ymean N, labels("Mean Y" "Observations") ///
	fmt(2 0)) varlabels( , blist(sim_unemp "\midrule \it{\underline{C: Triple Differences, State*Year*Kids FE}} \\ "))
eststo clear


***********************************************************************
*** Table 5: Heterogeneity of Effects by Economic Conditions - BRFSS***
***********************************************************************
use "${regdata}/brfss_reg.dta", clear

local controls female married urate uratesq uratecu avweek*
local absvars1 i.age i.race i.edgr i.imonth i.kids i.year i.statefip i.statefip#c.trend
local absvars2 i.age i.race i.edgr i.imonth  i.kids_unemp i.year_unemp i.state_unemp i.statefip#c.trend i.state_year_kids 

*** Unemployed
preserve
keep if unemployed==1
foreach y in plan check_lasty nafford genhlth goodgenhlth excgenhlth  {
	eststo: reghdfe `y' sim_ur50 sim_ur00 urate_00pc `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')	
	estadd ysumm
	test sim_ur50==sim_ur00 	
	estadd scalar p_equal = r(p)
}
esttab using "${out_tab}/p5_genhealth_ur.tex", replace keep(sim_*) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) label nonum nonotes noconstant mtitle("Coverage" "Checkup" "Afford Doctor" "Continuous" ///
	"Good" "Excellent") mgroups("Insurance" "Utilization" "General Health Status", pattern(1 1 0 1 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	varlabels( , blist(sim_ur50 "\midrule \it{\underline{A: State, Year Fixed Effects: Unemployed}} \\ ")) ///
	stats(p_equal N, labels ("P-value UR Low=High" "Observations") fmt(3 0)) postfoot("") prefoot("")
eststo clear
restore

*** DDD: With state-year-kids fe
foreach y in plan check_lasty nafford genhlth goodgenhlth excgenhlth  {
		eststo: reghdfe `y' sim_une_ur50 sim_une_ur00 unemp_ur50 unemp_ur00 `controls' [pw=finalwt], ///
			vce(cluster statefip) absorb(`absvars2') 
		estadd ysumm	
		test sim_une_ur50==sim_une_ur00	
		estadd scalar p_equal = r(p)
}
esttab using "${out_tab}/p5_genhealth_ur.tex", append keep(sim_*) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) label nonum nonotes noconstant nomtitle posthead("") prehead("") prefoot("") ///
	stats(p_equal N, labels ("P-value UR Low=High" "Observations") fmt(3 0)) ///
	varlabels( , blist(sim_une_ur50 "\midrule \it{\underline{B: Triple Differences, State*Year*Kids FE}} \\ "))
eststo clear
			
			
**********************************************************************************
******************************* TABLES IN APPENDIX *******************************
**********************************************************************************


***********************************************************
*** Table A1: Sample Summary Statistics - BRFSS and SIPP***
***********************************************************
use "${regdata}/sum_data.dta", clear

estpost tabstat age female black college married children real_wage [aw=weighting], by(group1) listwise s(mean sd) nototal columns(statistics)
esttab using "${out_tab}/p6_dems_sumstats.tex", replace main(mean) aux(sd) tex nostar unstack nogaps ///
	prefoot("") postfoot("") nodepvars nonumber noobs label nonotes varlabels( , blist(age "\midrule \it{\underline{A: Demographics}} \\ "))
eststo clear	

estpost tabstat uiamt insurance priv_hins pub_hins check_lasty nafford bexamly goodgenhlth goodphyshlth goodmenthlth ///
	high_bmi any_exer daily_smoker alcdays drinkge5 total [aw=weighting], by(group1) listwise s(mean sd) nototal columns(statistics)
esttab using "${out_tab}/p6_dems_sumstats.tex", append main(mean) aux(sd) tex nostar unstack nogaps posthead("") prehead("") prefoot("") ///
	nodepvars nonumber noobs label nonotes varlabels( , blist(uiamt "\midrule \it{\underline{B: Outcomes}} \\ "))


*********************************************************************
*** Table A2: Effect of State Economics Conditions on UI Generosity***
*********************************************************************
use "${regdata}/state_data.dta", clear
keep if year>=1993 & year<=2015
gen trend = year-1993

*** Rescale variables
for any urate epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: replace X=X*100	
replace avweekwage=avweekwage/10
for any urate avweekwage epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: gen Xsq=X*X
for any urate avweekwage epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: gen Xcu=X*X*X
replace st_sim_repl_sipp=st_sim_repl_sipp*100
rename st_sim_repl_sipp st_rrate

*** Label variables
label var urate "Unemployment Rate (\%)"
label var uratesq "Unemployment Rate\textsuperscript{2}"
label var uratecu "Unemployment Rate\textsuperscript{3}"
label var avweekwage "Average Weekly Wage (\\$2015, 1000s)"
label var avweekwagesq "Average Weekly Wage\textsuperscript{2}"
label var avweekwagecu "Average Weekly Wage\textsuperscript{3}"
label var epop "Employment Rate  (\%)"
label var epopsq "Employment Rate\textsuperscript{2}"
label var epopcu "Employment Rate\textsuperscript{3}"
for any 1 2: label var urate_lagX "X Lag Unemployment Rate (\%)"
for any 1 2: label var urate_lagXsq "X Lag Unemployment Rate\textsuperscript{2}"
for any 1 2: label var urate_lagXcu "X Lag Unemployment Rate\textsuperscript{3}"
for any 1 2: label var ui_netres_lagX_pop "X Lag (UI Net Reserves/Population) (\%)"
for any 1 2: label var ui_netres_lagX_popsq "X Lag (UI Net Reserves/Population)\textsuperscript{2}"
for any 1 2: label var ui_netres_lagX_popcu "X Lag (UI Net Reserves/Population)\textsuperscript{3}"


*** Create table
local controls i.statefip*trend i.year

foreach x in urate avweekwage epop {
	eststo: xi: reg st_rrate `x' `controls' [aw=population], vce(cluster statefip)
	estadd ysumm					

	eststo: xi: reg st_rrate `x' `x'sq `x'cu `controls' [aw=population], vce(cluster statefip)
	estadd ysumm	
}
eststo: xi: reg st_rrate urate uratesq uratecu avweekw* epop*  `controls' [aw=population], vce(cluster statefip)
estadd ysumm
esttab using "${out_tab}/p7_econditions_rrate.tex", replace tex keep(*urat* epop* avwe*) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) ///
	nonotes noconstant nonum mtitle("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)") stats(ymean r2 N, ///
	labels ("Mean R-rate" "R-squared" "Observations") fmt(2 3 0)) nodep
eststo clear


**************************************************************
*** Table A3: Effect of UI Generosity on Safety Net Programs***
**************************************************************
use "${regdata}/state_data.dta", clear
keep if year>=1993 & year<=2015
gen trend = year-1993

*** Rescale variables
replace medthresh=medthresh/100
for any urate avweekwage: gen Xsq=X*X
for any urate avweekwage: gen Xcu=X*X*X
rename st_sim_repl_sipp st_rrate
label var st_rrate "State R-rate"

local controls i.statefip*trend i.year urate uratesq uratecu avweekwage*

*** Generosity
foreach y in minwage eitc_val afdcmax4 medthresh reform st_max {
	eststo: xi: reg `y' st_rrate `controls'  [aw=population], vce(cluster statefip)
	estadd ysumm					
	sum st_rrate [aw=population]
	estadd scalar xmean = r(mean)
}
esttab using "${out_tab}/p8_snet_laws_rrate.tex", replace tex keep(st_rrate) se(3) b(3) label  star(* 0.10 ** 0.05 *** 0.01) ///
	nonotes noconstant nonum mtitle("Min Wage" "State EITC" "Max AFDC" "Medicaid Thresh" "Welfare Reform" "Max UI") ///
	stats(ymean xmean N, labels ("Mean Y" "Mean R-rate" "Observations") fmt(2 2 0)) nodep postfoot("") ///
	prefoot("") varlabels( , blist(minwage "\midrule \it{\underline{A: State Program Parameters}} \\ "))
eststo clear	

*** Spending per capita
foreach y in snap_pop eitc_pop tanf_pop medicaid_pop retdi_pop ui_benefits_pop {
	replace `y'=`y'*100
	eststo: xi: reg `y' st_rrate  `controls' [aw=population], vce(cluster statefip)
	estadd ysumm					
	sum st_rrate [aw=population]
	estadd scalar xmean = r(mean)
}
esttab using "${out_tab}/p8_snet_laws_rrate.tex", append tex keep(st_*) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) ///
	nonotes noconstant nonum mtitle("SNAP" "EITC" "ADFC" "Medicaid" "SS" "UI") posthead("") prehead("") prefoot("") ///
	stats(ymean xmean N, labels ("Mean Y" "Mean R-rate" "Observations") fmt(3 2 0)) nodep varlabels( , blist(st_rrate ///
	"\midrule \it{\underline{B: State Spending/Population}} \\ "))
eststo clear	


*******************************************************************************************************************************************
*** Table A4: Effect of UI Generosity on UI Benefits and Insurance Coverage - SIPP - Sensitivity to Simulated Replacement Rate Variation***
*******************************************************************************************************************************************
use "${regdata}/sipp_reg.dta", clear
xtset uniqueid order
for any agegr female white: egen Xloss=group(X jobloss)
label var sim_post "R-rate * After Loss"

local controls i.age female i.race i.edgr married i.imonth urate uratesq uratecu avweek* i.statefip_m*trend ///
		i.kids_mloss i.year_mloss i.statefip_mloss i.agegrloss i.femaleloss 
local mergy kids year statefip
local merggen kids female year statefip
local mergage kids agegr year statefip
local mergagend kids agegr female year statefip
		
*********************
*** REGRESSIONS - LOOPS BY UI
*********************	
foreach x in y gen age agend {
	preserve
	
	* Simulated instruments
	for any statefip kids year: rename X_m0 X
	drop sim_repl_sipp*
	merge m:1 `merg`x'' using "${regdata}/instrument_sipp_`x'", gen(instm) keepus(sim_repl_sipp*)
	keep if instm==3
	drop instm	
	label var sim_repl_sipp "Sim. R-Rate"
	for any statefip kids year: rename X X_m0
	replace sim_post=sim_repl_sipp if month_reljl>=0
	
	foreach y in uiamt any_hins priv_hins pub_hins {
		eststo: xi: xtreg `y' sim_post post `controls'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)
		sum sim_repl_sipp [aw=p5wgt_m0] if e(sample)==1
		estadd scalar xmean = r(mean)	
		quietly test _b[sim_post]=0
		estadd scalar pval = r(p)
	}
	if "`x'"=="y" esttab using "${out_tab}/p9_reg_sipp_instrs_rr.tex", replace keep(sim_*) se(3) b(3) ///
		label star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant mtitle("Benefit" "Any" "Private" "Public")  ///
		mgroups("Monthly UI""Health Insurance", pattern(1 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span})) stats(xmean pval, labels ("Mean R-rate" "P-Value") fmt(3 3)) postfoot("") ///
		varlabels( , blist(sim_post "\midrule \it{\underline{A: Baseline}} \\ ")) 
		
	if "`x'"=="gen" esttab using "${out_tab}/p9_reg_sipp_instrs_rr.tex", append keep(sim_*) se(3) b(3) ///
		label star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nomtitle stats(xmean pval, labels ("Mean R-rate" "P-Value") fmt(3 3)) ///
		postfoot("") posthead("") prehead("") varlabels( , blist(sim_post "\midrule \it{\underline{B: Add Gender}} \\ ")) 
	
	if "`x'"=="age" esttab using "${out_tab}/p9_reg_sipp_instrs_rr.tex", append keep(sim_*) se(3) b(3) ///
		label star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nomtitle stats(xmean pval, labels ("Mean R-rate" "P-Value") fmt(3 3)) ///
		postfoot("") posthead("") prehead("") varlabels( , blist(sim_post "\midrule \it{\underline{C: Add Age Group}} \\ ")) 
	
	if "`x'"=="agend" esttab using "${out_tab}/p9_reg_sipp_instrs_rr.tex", append keep(sim_*) se(3) b(3) ///
		label star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nomtitle stats(xmean pval, labels ("Mean R-rate" "P-Value") fmt(3 3)) ///
		posthead("") prehead("") varlabels( , blist(sim_post "\midrule \it{\underline{D: Add Age Group and Gender}} \\ ")) //postfoot("") 		
	eststo clear

	restore
}


*************************************************************************************************************************
*** Table A5: Effect of UI Generosity on UI Benefits and Insurance Coverage - Sensitivity to Dropping 2008-2010 - SIPP***
*************************************************************************************************************************
use "${regdata}/sipp_reg.dta", clear
xtset uniqueid order

local controls i.age female i.race i.edgr married i.imonth urate uratesq uratecu avweek* i.kids_mloss i.year_mloss ///
	i.statefip_mloss i.statefip_m*trend

*** Drop recession period
* Baseline
foreach y in uiamt any_hins {
	eststo: xi: xtreg `y' sim_post post `controls'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
}

* Drop observations in 2008-2010
foreach y in uiamt any_hins {
	eststo: xi: xtreg `y' sim_post post `controls'  if year_m<=2007 | year_m>=2011 [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
}

* Drop losers in 2008-2010
foreach y in uiamt any_hins {
	eststo: xi: xtreg `y' sim_post post `controls'  if year_m0<=2007 | year_m0>=2011 [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
	estadd ysumm
}

esttab using "${out_tab}/p10_reg_sipp_grrec.tex", replace keep(sim_*) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) nonum ///
	nonotes noconstant mtitle("Monthly UI" "Any Insurance" "Monthly UI" "Any Insurance" "Monthly UI" "Any Insurance") ///
	mgroups("Baseline Sample" "Drop 2008-2010 Observations" "Drop 2008-2010 Job Losers", pattern(1 0 1 0 1 0 )) ///
	stats(ymean N, labels ("Mean Y" "Observations") fmt(2 0))
eststo clear


***********************************************************************************************
*** Table A6: Placebo Effect of UI Generosity on UI Benefits and Insurance coverage - Placebo***
***********************************************************************************************
local controls i.age female i.race i.edgr married i.imonth urate uratesq uratecu avweek* i.kids_mloss i.year_mloss ///
	i.statefip_mloss i.statefip_m*trend

forvalues sub = 0/2 {
	if `sub'==0 use "${regdata}/sipp_reg.dta", clear
	else use "${regdata}/sipp_reg_plac`sub'.dta", clear
	xtset uniqueid order
	
	foreach y in uiamt any_hins {
		xi: xtreg `y' sim_post post `controls'  [pw=p5wgt_m0], fe vce(cluster statefip_m0)	
		estimates store r`y'`sub'
		estadd ysumm
	}
}
esttab ruiamt0 rany_hins0 ruiamt1 rany_hins1 ruiamt2 rany_hins2 using "${out_tab}/p11_reg_sipp_plac.tex", ///
	replace keep(sim_*) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant mtitle("Monthly UI" "Any Insurance" ///
	"Monthly UI" "Any Insurance" "Monthly UI" "Any Insurance") ///
	mgroups("Involuntary Job Loss" "Quit For Another Job" "Other Separations", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) ///
	span erepeat(\cmidrule(lr){@span})) stats(ymean N, labels ("Mean Y" "Observations") fmt(2 0))
eststo clear



*******************************************************************
*** Table A7: Effect of UI Generosity on Risky Behaviors - BRFSS***
*******************************************************************
use "${regdata}/brfss_reg.dta", clear

local controls female married urate uratesq uratecu avweek*
local absvars1 i.age i.race i.edgr i.imonth i.kids i.year i.statefip i.statefip#c.trend
local absvars2 i.age i.race i.edgr i.imonth  i.kids_unemp i.year_unemp i.state_unemp i.statefip#c.trend
	 
*** Unemployed
preserve
keep if unemployed==1

foreach y in high_bmi norm_bmi smoking daily_smoker alcdays drinkge5 pregnant {
	eststo: reghdfe `y' sim_repl_sipp `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
	estadd ysumm	
}
esttab using "${out_tab}/p12_behav_main.tex", replace keep(sim_repl*) star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) ///
	nonum nonotes noconstant label mtitle("High" "Normal" "Sometimes" "Daily" "Days" ///
	"Days w/ 5" "Now") mgroups("BMI" "Smoking" "Alcohol" "Pregnant", pattern(1 0 1 0 1 0 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) prefoot("") ///
	postfoot("") stats(ymean N, labels("Mean Y" "Observations") fmt(2 0)) varlabels( , ///
	blist(sim_repl_sipp "\midrule \it{\underline{A: State, Year Fixed Effects: Unemployed}} \\ "))	
eststo clear
restore

*** DDD: With state-year fe
foreach y in high_bmi norm_bmi smoking daily_smoker alcdays drinkge5 pregnant {
	eststo: reghdfe `y' sim_repl_sipp sim_unemp unemployed `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars2' i.state_year_kids) 
	estadd ysumm	
	sum sim_repl_sipp if `y'!=. [aw=finalwt]
	estadd scalar xmean = r(mean)
	estadd scalar xsd = r(sd)
}
esttab using "${out_tab}/p12_behav_main.tex", append keep(sim_unemp) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) nonum nonotes noconstant nomtitle label posthead("") prehead("") ///
	prefoot("") stats(ymean N, labels("Mean Y" "Observations") ///
	fmt(2 0)) varlabels( , blist(sim_unemp "\midrule \it{\underline{B: Triple Differences, State*Year*Kids FE}} \\ "))
eststo clear


*****************************************************************
*** Table A8: Heterogeneity of Effects by Demographics - BRFSS***
*****************************************************************
use "${regdata}/brfss_reg.dta", clear
keep if unemployed==1

local controls female married i.edgr urate uratesq uratecu avweek*
local absvars1 i.age i.race i.edgr i.imonth i.kids i.year i.statefip i.statefip#c.trend
local absvars2 i.age i.race i.edgr i.imonth  i.kids_unemp i.year_unemp i.state_unemp i.statefip#c.trend

foreach y in plan check_lasty nafford genhlth {	
	
	* All
	eststo: reghdfe `y' sim_repl_sipp `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
	estadd ysumm	
	
	foreach gr in female married college childless genmed {
		eststo: reghdfe `y' sim_repl_sipp sim_`gr' `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
		estadd ysumm		
	}
	
	if "`y'"=="plan" esttab using "${out_tab}/p13_heterogeneity_dd.tex", replace keep(sim_*) star(* 0.10 ** 0.05 *** 0.01) ///
		se(3) b(3) label numbers nonotes noobs noconstant nomtitles postfoot("") prefoot("") varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{A: Insurance Coverage}} \\ "))
	
	if "`y'"=="check_lasty" esttab using "${out_tab}/p13_heterogeneity_dd.tex", append keep(sim_*) star(* 0.10 ** 0.05 *** 0.01) ///
		se(3) b(3) nonum nonotes noobs noconstant nomtitles label prehead("") posthead("") postfoot("") ///
		prefoot("")  varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{B: Check-up}} \\ "))
		
	if "`y'"=="genhlth" esttab using "${out_tab}/p13_heterogeneity_dd.tex", append keep(sim_*) star(* 0.10 ** 0.05 *** 0.01) ///
		se(3) b(3) nonum nonotes noobs noconstant nomtitles label prehead("") posthead("") ///
		varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{C: General Health Status} \\ "))
	eststo clear	
}


***************************************************************************
*** Table A9: Sensitivity of Effects to Measure of UI Generosity - BRFSS***
***************************************************************************
use "${regdata}/brfss_reg.dta", clear
keep if unemployed==1

local controls female married urate uratesq uratecu avweek*
local absvars1 i.age i.race i.edgr i.imonth i.kids i.year i.statefip i.statefip#c.trend

*** Simulated Instrument
foreach y in plan check_lasty nafford genhlth goodgenhlth excgenhlth {
	eststo: reghdfe `y' sim_repl_sipp `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
	estadd ysumm	
	sum sim_repl_sipp if `y'!=. [aw=finalwt]
	estadd scalar xmean = r(mean)	
}
esttab using "${out_tab}/p14_minmax.tex", replace keep(sim_repl*) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) nonum nonotes noconstant label mtitle("Insurance" "Checkup" "Afford Doctor" "Continuous" ///
	"Good" "Excellent") mgroups("Utilization" "General Health", pattern(1 0 0 1 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) prefoot("") ///
	postfoot("") stats(xmean, labels("Mean R-rate") fmt(2)) ///
	varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{A: Simulate R-rate}} \\ "))
eststo clear

*** Max WBA
foreach y in plan check_lasty nafford genhlth goodgenhlth excgenhlth {
	eststo: reghdfe `y'  max_cpi `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
	estadd ysumm	
	sum  max_cpi if `y'!=. [aw=finalwt]
	estadd scalar xmean = r(mean)	
}
esttab using "${out_tab}/p14_minmax.tex", append keep(max_cpi) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) nonum nonotes noconstant nomtitle label prehead("") ///
	posthead("") prefoot("") postfoot("") stats(xmean, labels ("Mean Max") fmt(2)) ///
	varlabels( , blist(max_cpi "\midrule \it{\underline{B: Maximum Benefit Level}} \\ "))
eststo clear

*** Min WBA
foreach y in plan check_lasty nafford genhlth goodgenhlth excgenhlth {
	eststo: reghdfe `y'  min_cpi `controls' [pw=finalwt], vce(cluster statefip) absorb(`absvars1')
	estadd ysumm	
	sum  min_cpi if `y'!=. [aw=finalwt]
	estadd scalar xmean = r(mean)	
}	
esttab using "${out_tab}/p14_minmax.tex", append keep(min_cpi) star(* 0.10 ** 0.05 *** 0.01) ///
	se(3) b(3) nonum nonotes noconstant nomtitle label posthead("") ///
	prehead("") prefoot("") stats(xmean, labels ("Mean Min") fmt(2)) ///
	varlabels( , blist(min_cpi "\midrule \it{\underline{C: Minimum Benefit Level}} \\ "))
eststo clear

***********************************************************************************
*** Table A19: Effects of UI Generosity on Sample Characteristics - BRFSS and SIPP***
***********************************************************************************
use "${regdata}/brfss_reg.dta", clear

cap drop num
gen num=1
gen num_unemp = unemployed==1
gen num_emp = unemployed==0

***Part 1: BRFSS
local stcontrols urate uratesq uratecu avweek* i.kids i.year i.statefip*trend 

foreach z in 1 0 {
	
	* Share of Respondents
	preserve
	collapse (mean) sim_repl_sipp urate* avweek* trend (sum) num num_unemp num_emp (rawsum) finalwt [pw=finalwt], by(year statefip kids)
	gen unemployed = num_unemp/num
	gen employed = num_emp/num
	
	if `z'==1 eststo: xi: reg unemployed sim_repl_sipp `stcontrols' [aw=finalwt], vce(cluster statefip)	
	if `z'==0 eststo: xi: reg employed sim_repl_sipp `stcontrols' [aw=finalwt], vce(cluster statefip)
	estadd ysumm	
	restore
	
	* Demographics
	preserve
	keep if unemployed==`z'
	
	foreach y in female married age lesshs college white black {
		eststo: xi: reg `y' sim_repl_sipp `stcontrols' [pw=finalwt], vce(cluster statefip)	
		estadd ysumm
	}
	restore
	
	if `z'==1 esttab using "${out_tab}/p15_check_dems_rr.tex", replace keep(sim_repl_sipp) se(3) b(3) label ///
		star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant mtitle("Sample Share of Total" "Female" "Married" "Age" "Less HS" "College" ///
		"White" "Black") mgroups("" "Sample Demographics", pattern(1 1 0 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) ///
		span erepeat(\cmidrule(lr){@span})) stats(ymean N, labels ("Mean Y" "Observations") fmt(3 0)) prefoot("") postfoot("") ///
		varlabels( , blist(sim_repl_sipp "\midrule \multicolumn{3}{l}{\it{\underline{A: Unemployed, BRFSS}}} \\ "))
		
	if `z'==0 esttab using "${out_tab}/p15_check_dems_rr.tex", append keep(sim_repl_sipp) se(3) b(3) label ///
		star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nomtitle prefoot("") postfoot("") prehead("") posthead("") ///
		stats(ymean N, labels ("Mean Y" "Observations") fmt(3 0)) varlabels( , blist(sim_repl_sipp ///
		"\midrule \multicolumn{3}{l}{\it{\underline{B: Employed, BRFSS}}} \\ "))
		
	eststo clear	
}

*** Part 2: SIPP
* Create additional variables
use "${regdata}/sipp_reg.dta", clear
xtset uniqueid order

cap drop num
gen num=1
gen num_unemp = month_reljl==0
gen num_emp = month_reljl!=0
label var sim_repl_sipp "R-rate"

* Panel (c)
preserve
collapse (rawsum) p5wgt_m0 (sum) num (mean) num_unemp sim_repl_sipp urate uratesq uratecu avweek* trend [pw=p5wgt_m0], by(statefip_m0 kids_m0 year_m0)
eststo: xi: reg num_unemp sim_repl_sipp urate uratesq uratecu avweek* i.kids_m i.year_m i.statefip_m*trend [aw=p5wgt_m0], vce(cluster statefip_m0)	
estadd ysumm 
restore

foreach y in female married age lesshs college white black {
	eststo: xi: reg `y' sim_repl_sipp i.imonth urate uratesq uratecu avweek* i.kids_m i.year_m i.statefip_m*trend if month_reljl==0 [pw=p5wgt_m0], vce(cluster statefip_m0)	
	estadd ysumm 
}
esttab using "${out_tab}/p15_check_dems_rr.tex", append keep(sim_repl_sipp) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) ///
	nonum nonotes noconstant nomtitle prefoot("") postfoot("") prehead("") posthead("") stats(ymean N, labels ("Mean Y" "Observations") ///
	fmt(3 0)) varlabels( , blist(sim_repl_sipp "\midrule \multicolumn{3}{l}{\it{\underline{C: Job Losers at Time of Job Loss, SIPP}}} \\ "))
eststo clear

* Panel (d)
preserve
drop num_unemp 
gen num_unemp = month_reljl>=0
collapse (rawsum) p5wgt_m0 (sum) num (mean) num_unemp sim_repl_sipp urate uratesq uratecu avweek* trend [pw=p5wgt_m0], by(statefip_m0 kids_m0 year_m0)
eststo: xi: reg num_unemp sim_repl_sipp urate uratesq uratecu avweek* i.kids_m i.year_m i.statefip_m*trend [aw=p5wgt_m0], vce(cluster statefip_m0)	
estadd ysumm 
restore

foreach y in female married age lesshs college white black {
	eststo: xi: reg `y' sim_repl_sipp i.imonth urate uratesq uratecu avweek* i.kids_m i.year_m i.statefip_m*trend if month_reljl>=0 [pw=p5wgt_m0], vce(cluster statefip_m0)	
	estadd ysumm 
}
esttab using "${out_tab}/p15_check_dems_rr.tex", append keep(sim_repl_sipp) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) ///
	nonum nonotes noconstant nomtitle prefoot("") prehead("") posthead("") stats(ymean N, labels ("Mean Y" "Observations") ///
	fmt(3 0)) varlabels( , blist(sim_repl_sipp "\midrule \multicolumn{3}{l}{\it{\underline{D: Job Losers after Job Loss, SIPP}}} \\ "))
eststo clear


*****************************************************************************
*** Table C1: Effect of State Lagged Economics Conditions on UI Generosity***
*****************************************************************************
use "${regdata}/state_data.dta", clear
keep if year>=1993 & year<=2015
gen trend = year-1993

for any urate epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: replace X=X*100		
for any urate avweekwage epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: gen Xsq=X*X
for any urate avweekwage epop urate_lag1 urate_lag2 ui_netres_lag1_pop ui_netres_lag2_pop: gen Xcu=X*X*X
replace avweekwage=avweekwage/10
replace st_sim_repl_sipp=st_sim_repl_sipp*100
rename st_sim_repl_sipp st_rrate

for any 1 2: label var urate_lagX "X Lag Unemployment Rate (\%)"
for any 1 2: label var urate_lagXsq "X Lag Unemployment Rate\textsuperscript{2}"
for any 1 2: label var urate_lagXcu "X Lag Unemployment Rate\textsuperscript{3}"
for any 1 2: label var ui_netres_lagX_pop "X Lag (UI Net Reserves/Population) (\%)"
for any 1 2: label var ui_netres_lagX_popsq "X Lag (UI Net Reserves/Population)\textsuperscript{2}"
for any 1 2: label var ui_netres_lagX_popcu "X Lag (UI Net Reserves/Population)\textsuperscript{3}"

local controls i.statefip*trend i.year

*** Add urate lags
eststo: xi: reg st_rrate urate_lag1* `controls' [aw=population], vce(cluster statefip)
estadd ysumm	

eststo: xi: reg st_rrate urate_lag1* urate_lag2* `controls' [aw=population], vce(cluster statefip)
estadd ysumm	

*** Add UI reserve lags
eststo: xi: reg st_rrate ui_netres_lag1_pop* `controls' [aw=population], vce(cluster statefip)
estadd ysumm	

*** Add UI reserve lags
eststo: xi: reg st_rrate ui_netres_lag1_pop* ui_netres_lag2_pop* `controls' [aw=population], vce(cluster statefip)
estadd ysumm	

*** Add all urates	that seem to matter
eststo: xi: reg st_rrate urate avweekwage urate_lag1* urate_lag2* ui_netres_lag1_pop* ui_netres_lag2_pop* `controls' [aw=population], vce(cluster statefip)
	estadd ysumm

esttab using "${out_tab}/p16_econditions_app_rrate.tex", replace tex keep(*urat* avwe* ui*) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) ///
	nonotes noconstant nonum mtitle("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)" "(9)" "(10)") stats(r2 N, ///
	labels ("R-squared" "Observations") fmt(3 0)) nodep
eststo clear	


*******************************************************************************************************************
*** Table C2: Effect of State Economics Conditions on UI Generosity - State-Year_Number of Children Observations***
*******************************************************************************************************************
use "${regdata}/state_kids_data.dta", clear

*** Urate and Epop 
eststo: xi: reg sim_repl_sipp urate_ip i.statefip*trend i.year i.kids [aw=weight_ur], vce(cluster statefip)
	estadd ysumm
	
foreach x in urate_ip epop_ip {
	eststo: xi: reg sim_repl_sipp `x' `x'sq `x'cu i.statefip*trend i.year i.kids [aw=weight_ur], vce(cluster statefip)
	estadd ysumm				
}

*** Add lags
eststo: xi: reg sim_repl_sipp urate_ip_lag1* i.year i.kids i.statefip*trend [aw=weight_ur], vce(cluster statefip)
estadd ysumm	

eststo: xi: reg sim_repl_sipp urate_ip_lag1* urate_ip_lag2* i.year i.kids i.statefip*trend [aw=weight_ur], vce(cluster statefip)
estadd ysumm	

*** Add UI reserve lags
eststo: xi: reg sim_repl_sipp ui_netres_lag1* i.year i.kids i.statefip*trend [aw=weight_ur], vce(cluster statefip)
estadd ysumm	

eststo: xi: reg sim_repl_sipp ui_netres_lag1* ui_netres_lag2* i.year i.kids i.statefip*trend [aw=weight_ur], vce(cluster statefip)
estadd ysumm	

*** Add all urates that seem to matter
eststo: xi: reg sim_repl_sipp urate_ip urate_ipsq urate_ipcu urate_ip_lag1* urate_ip_lag2* ui_netres_lag1* ui_netres_lag2* ///
	i.year i.kids i.statefip*trend [aw=weight_ur], vce(cluster statefip)
	estadd ysumm

esttab using "${out_tab}/p17_econditions_stkids.tex", replace tex keep(*urat* epop* ui*) se(3) b(3) label star(* 0.10 ** 0.05 *** 0.01) ///
	nonotes noconstant nonum mtitle("" "" "" "" "" "" "" "") stats(ymean r2 N, labels ("Mean R-rate" "R-Squared" ///
	"Observations") fmt(2 3 0)) nodep 
eststo clear


***************************************************************
*** Table C3: Effect of UI Generosity on Safety Net Spending***
***************************************************************
use "${regdata}/state_kids_data.dta", clear

*** Rescale variables
replace sim_repl_sipp = sim_repl_sipp/100
for any urate_ip epop_ip urate_ip_lag1 urate_ip_lag2 ui_netres_lag1 ui_netres_lag2: replace X=X/100	
for any urate_ip epop_ip urate_ip_lag1 urate_ip_lag2 ui_netres_lag1 ui_netres_lag2: replace Xsq=X*X
for any urate_ip epop_ip urate_ip_lag1 urate_ip_lag2 ui_netres_lag1 ui_netres_lag2: replace Xcu=X*X*X

local controls i.statefip*trend i.kids i.year urate_ip urate_ipsq urate_ipcu avweekwage*

foreach type in cps sipp_imp sipp  {
	foreach y in fs_`type'_pop tanf_`type'_pop ssi_`type'_pop ss_`type'_pop ui_`type'_pop {
		eststo: xi: reg `y' sim_repl_sipp `controls' [aw=weight_pop], vce(cluster statefip)
		estadd ysumm					
		sum sim_repl_sipp if sim_repl_sipp!=. [aw=weight_pop]
		estadd scalar xmean = r(mean)	
	}
	label var sim_repl_sipp "R-rate"

	if "`type'"=="cps" esttab using "${out_tab}/p18_spending_stkids.tex", replace tex keep(sim_repl_sipp) se(3) b(3) label ///
		star(* 0.10 ** 0.05 *** 0.01) nonotes noconstant nonum mtitle("SNAP" "ADFC" "SSI" "SS" "UI") stats(ymean xmean N, ///
		labels ("Mean Y" "Observations") fmt(2 0)) nodep postfoot("") prefoot("") varlabels( , blist(sim_repl_sipp ///
		"\midrule \it{\underline{A: CPS Data}} \\ "))
	
	if "`type'"=="sipp_imp" esttab using "${out_tab}/p18_spending_stkids.tex", append tex keep(sim_repl_sipp) se(3) b(3) label ///
		star(* 0.10 ** 0.05 *** 0.01) nonotes noconstant nonum mtitle("" "" "" "" "" "")  stats(ymean xmean N, labels ///
		("Mean Y" "Observations") fmt(2 0)) nodep posthead("") prehead("") prefoot("") postfoot("") ///
		varlabels( , blist(sim_repl_sipp "\midrule \it{\underline{B: SIPP Data}} \\ "))
	eststo clear	
}
